﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Resources;
using System.Reflection;

namespace DZ
{
    public class ClsCommon
    {

        /// <summary>
        /// This function creates a errorlog folder at path of exe if already does not exist. 
        /// And creates "ExceptionLogFile" or "ComLogFile" based on argument "IsExceptionLog" and logs error in file.
        /// </summary>
        public static void ErrorLog(String str, bool IsExceptionLog = false)
        //Comment add in particular Variables like path , folderpath etc.
        {
            DateTime CurrentTime = DateTime.Now;
            str = CurrentTime.ToString() + "    :-" + str + "\n";
            String FileType;
            if (IsExceptionLog)
            {
                FileType = "ExceptionLogfile";
            }
            else
            {
                FileType = "ComLogfile";
            }
            String FileName = FileType + CurrentTime.Day.ToString() + "-" + CurrentTime.Month.ToString() + "-" + CurrentTime.Year.ToString();
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            String FolderPath = System.IO.Path.GetDirectoryName(path);
            FolderPath += "\\ErrorLog";
            if (!System.IO.Directory.Exists(FolderPath))
            {
                System.IO.Directory.CreateDirectory(FolderPath);
            }
            FileName = FolderPath + "\\" + FileName + ".txt";
            System.IO.StreamWriter file = new System.IO.StreamWriter(FileName, true);
            file.Write(str);
            file.Write(file.NewLine);
            file.Close();
        }
        private string GetCurrentTime()
        {
            return System.DateTime.Now.ToString("HH:MM:ss") + ":" + System.DateTime.Now.Millisecond;
        }
        public int GetComboValueItem(ref System.Windows.Controls.ComboBox CmbToGetValue)
        {
            try
            {
                int valueToReturn = -1;
                if (CmbToGetValue.Items.Count == 0)
                    return valueToReturn;
                if (CmbToGetValue.SelectedIndex == -1)
                    return valueToReturn;
                DataItem sh = new DataItem((DataItem)CmbToGetValue.Items[CmbToGetValue.SelectedIndex]);
                valueToReturn = sh.Key;
                return valueToReturn;
            }
            catch (Exception ex)
            {
                ClsCommon.ErrorLog(ex.ToString());
                return 0;
            }
        }
        
    }
}
